
Page({
  data: {
    user: null,
    learningMinutes: 0,
    showModal: false
  },
  onLoad() {
    this.getUserInfoById();
  },
  onShow() {
    // Check if the previous page has set the refresh flag
    const shouldRefresh = wx.getStorageSync('refreshPreviousPage');
    if (shouldRefresh) {
        this.getUserInfoById();
        // Clear the flag after refreshing
        wx.removeStorageSync('refreshPreviousPage');
    }
  },
  getUserInfoById: function() {
    const userId = this.getUserId(); // 获取用户ID
    wx.request({
        url: 'http://127.0.0.1:5000/api/userinfo/' + userId,
        method: 'GET',
        success: (res) => {
            this.setData({
              user: res.data
            });
        },
        fail: (err) => {
            console.error('Failed to fetch courses:', err); // 调试信息
            wx.showToast({
                title: '获取课程失败',
                icon: 'none'
            });
        }
    });
  },
  navigateBack() {
    wx.navigateBack();
  },
  navToSettings() {
    console.log('跳转到设置页面');
    const { username, head_image } = this.data.user || {};
        wx.navigateTo({
          url: `/pages/user/settings/settings?username=${encodeURIComponent(username)}&head_image=${encodeURIComponent(head_image)}`
        });
    },
  //  // 获取用户信息
  //  getUserProfile() {
  //   const that = this;
  //   wx.getUserProfile({
  //     desc: '用于完善会员资料', // 提示用户获取信息的用途
  //     success: (res) => {
  //       console.log('用户信息：', res.userInfo);
  //       that.setData({
  //         user: res.userInfo, // 保存用户信息到 data 中
  //       });
  //     },
  //     fail: (err) => {
  //       console.error('用户拒绝授权：', err);
  //     },
  //   });
  // },
  navTo(event) {
    const url = event.currentTarget.dataset.url;
    wx.navigateTo({
        url: url
    });
},
  getUserId() {
    // 实现获取用户ID的逻辑
    // 例如，从缓存中获取用户ID
    return wx.getStorageSync('userId');
  },
  showQRCode: function() {
    this.setData({
        showModal: true
    });
  },
  hideQRCode: function() {
      this.setData({
          showModal: false
      });
  }
});